package org.digitarts.rtms.helpers;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;

public class FileHelper
{
	public static byte[] getBytesFromUrl(URL sourceUrl)
	{
		byte[] buffer = new byte[8192];
		int bytesRead = 0;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try
		{
			BufferedInputStream bis = new BufferedInputStream(sourceUrl.openStream());
			while (true)
			{
				bytesRead = bis.read(buffer);
				if (bytesRead == -1)
					break;
				baos.write(buffer, 0, bytesRead);
			}
			return baos.toByteArray();
		}
		catch (Exception e)
		{
			throw new RuntimeException("Failed to read bytes from URL [" + sourceUrl + "]", e);
		}
	}
}
